﻿using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Imaginecup.gameState.ending.scene;
using Imaginecup.gameState.ending.scene.kindOfAnimation;
using Imaginecup.gameState.ending.scene.kindOfNormal;
using System.Collections.Generic;
using Imaginecup.factory;

namespace Imaginecup.gameState.ending
{
    class GameEnding: GameState
    {
        enum SceneState { Scene1, Scene2, Scene3, Scene4, Scene5, Scene6, Scene7_1 , Scene7_2,  Scene8 ,  Scene_CloudFadeIn, 
            Scene9 , Scene10 , Scene_ThankFadeIn , Scene11};
        
        private ContentManager m_Content;
        private static GameEnding m_Ending = null;
        
        private IScene m_EndingScene;

        private Dictionary<SceneState, IScene> m_EndingSceneDictionary;

        private SceneState[] m_OrderSceneState = new SceneState[]{
            SceneState.Scene1,
            SceneState.Scene2,
            SceneState.Scene3,
            SceneState.Scene4,
            SceneState.Scene5,
            SceneState.Scene6,
            SceneState.Scene7_1,
            SceneState.Scene7_2,
            SceneState.Scene8,
            SceneState.Scene_CloudFadeIn,
            SceneState.Scene9,
            SceneState.Scene10,
            SceneState.Scene_ThankFadeIn,
            SceneState.Scene11
        };

        private int m_CurrentSceneIndex;

        private GameEnding()
        {
            m_Content = new ContentManager(m_Game.Services);
            m_Content.RootDirectory = "Content";


        }

        public static GameEnding GetEnding()
        {
            if (m_Ending == null)
            {
                m_Ending = new GameEnding();
            }
            return m_Ending;
        }

        /************************************************************************/
        /* 초기화 되는 값                                                       */
        /************************************************************************/
        protected override void InitState()
        {
            m_EndingSceneDictionary = new Dictionary<SceneState, IScene>();
            for (int i = 0; i < m_OrderSceneState.Length; i++)
            {
                SetScene(i);
            }


            m_CurrentSceneIndex = 0;
            m_EndingScene = m_EndingSceneDictionary[m_OrderSceneState[m_CurrentSceneIndex]];
            m_EndingScene.LoadEndingScene();
        }

        /************************************************************************/
        /* ContentMan에 리소스 로드(배경,메뉴 리소스)                                  */
        /************************************************************************/
        protected override void LoadStateResource()
        {
        }


        /************************************************************************/
        /* 리소스해제                                                                     */
        /************************************************************************/
        protected override void UnloadResource()
        {
            m_Content.Unload();
        }
        

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {

            m_EndingScene.Update(gameTime);

            if (m_EndingScene.IsComplete() )
            {
                m_CurrentSceneIndex++;
                if( m_CurrentSceneIndex >= m_OrderSceneState.Length )
                {
                    m_EndingScene.UnloadEndingScene();
                    ChangeState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_TitleMenu));
                    return;
                }

                ChangeScene(m_OrderSceneState[m_CurrentSceneIndex]);
            }

            base.Update(gameTime);
        }



        private void ChangeScene(SceneState Scene)
        {
            switch (Scene)
            {
                case SceneState.Scene1:
                case SceneState.Scene2:
                case SceneState.Scene3:
                case SceneState.Scene4:
                case SceneState.Scene5:
                case SceneState.Scene6:
                case SceneState.Scene7_1:
                case SceneState.Scene7_2:
                case SceneState.Scene8:
                case SceneState.Scene10:
                    m_EndingScene.UnloadEndingScene();
                    break;
                case SceneState.Scene_CloudFadeIn:
                    m_EndingSceneDictionary[SceneState.Scene9].LoadEndingScene();
                    break;
                case SceneState.Scene9:
                    //fade스테이트에서 초기화 하지 않았으므로..
                    m_EndingSceneDictionary[SceneState.Scene8].UnloadEndingScene();
                    m_EndingScene.UnloadEndingScene();// fadeScene해제
                    m_EndingScene = m_EndingSceneDictionary[Scene]; // 이미 fade에서 로드했으므로 필요없음.
                    return;

                case SceneState.Scene_ThankFadeIn:
                    m_EndingSceneDictionary[SceneState.Scene11].LoadEndingScene();
                    break;
                case SceneState.Scene11:
                    //fade스테이트에서 초기화 하지 않았으므로..
                    m_EndingSceneDictionary[SceneState.Scene10].UnloadEndingScene();
                    m_EndingScene.UnloadEndingScene();// fadeScene해제
                    m_EndingScene = m_EndingSceneDictionary[Scene]; // 이미 fade에서 로드했으므로 필요없음.
                    return;
            }

            m_EndingScene = m_EndingSceneDictionary[Scene];
            m_EndingScene.LoadEndingScene();
        }

        private void SetScene(int Num)
        {
            //Num으로 순서대로 초기화하는 이유는 Fade의 경우 다른 것을 참조하기때문....
            IScene scene = null;
            switch (Num)
            {
                case 0:
                    scene = new EndingNormalScene(0.5);
                    scene.SetLoadEndingScene(new NormalSpriteLoad("1", "ending\\1\\1"));
                    m_EndingSceneDictionary.Add(SceneState.Scene1, scene);
                    break;
                case 1:
                    scene = new EndingNormalScene(0.5);
                    scene.SetLoadEndingScene(new NormalSpriteLoad("1", "ending\\1\\2"));
                    m_EndingSceneDictionary.Add(SceneState.Scene2, scene);
                    break;
                case 2:
                    scene = new EndingAnimationScene();
                    scene.SetLoadEndingScene(new AnimationFlask());
                    m_EndingSceneDictionary.Add(SceneState.Scene3, scene);
                    break;
                case 3:
                    scene = new EndingAnimationScene();
                    scene.SetLoadEndingScene(new AnimationBlackFlask());
                    m_EndingSceneDictionary.Add(SceneState.Scene4, scene);
                    break;
                case 4:
                    scene = new EndingAnimationScene();
                    scene.SetLoadEndingScene(new AnimationCloudUp1());
                    m_EndingSceneDictionary.Add(SceneState.Scene5, scene);
                    break;
                case 5:
                    scene = new EndingAnimationScene();
                    scene.SetLoadEndingScene(new AnimationCloudUp2());
                    m_EndingSceneDictionary.Add(SceneState.Scene6, scene);
                    break;
                case 6:
                    scene = new EndingAnimationScene();
                    scene.SetLoadEndingScene(new AnimationGeneratorCloud());
                    m_EndingSceneDictionary.Add(SceneState.Scene7_1, scene);
                    break;
                case 7:
                    scene = new EndingNormalScene(1.5);
                    scene.SetLoadEndingScene(new NormalSpriteLoad("1", "ending\\3\\19"));
                    m_EndingSceneDictionary.Add(SceneState.Scene8, scene);
                    break;
                case 8:
                    scene = new CloudScene();
                    scene.SetLoadEndingScene(new NormalSpriteLoad("Cloud", "ending\\Cloud"));
                    m_EndingSceneDictionary.Add(SceneState.Scene9, scene);
                    break;

                case 9:
                    EndingFadeScene fadeForCloud = new EndingFadeScene();
                    fadeForCloud.SetFadeOutBackScene(m_EndingSceneDictionary[SceneState.Scene8], 2);
                    fadeForCloud.SetFadeInBackScene(m_EndingSceneDictionary[SceneState.Scene9], 2);

                    scene = fadeForCloud;
                    scene.SetLoadEndingScene(new FadeSpriteLoad(Color.Black));
                    m_EndingSceneDictionary.Add(SceneState.Scene_CloudFadeIn, scene);
                    break;
                case 10:
                     scene = new EndingAnimationScene();
                    scene.SetLoadEndingScene(new AnimationSmile());
                    m_EndingSceneDictionary.Add(SceneState.Scene10, scene);
                    break;
                case 11:
                    scene = new EndingNormalScene(2);
                    scene.SetLoadEndingScene(new NormalSpriteLoad("Thank" , "ending\\Thank"));
                    m_EndingSceneDictionary.Add(SceneState.Scene11, scene);
                    break;

                case 12:                    
                    EndingFadeScene fadeforThanks = new EndingFadeScene();
                    fadeforThanks.SetFadeOutBackScene(m_EndingSceneDictionary[SceneState.Scene10], 2);
                    fadeforThanks.SetFadeInBackScene(m_EndingSceneDictionary[SceneState.Scene11], 2);

                    scene = fadeforThanks;
                    scene.SetLoadEndingScene(new FadeSpriteLoad(Color.Black));
                    m_EndingSceneDictionary.Add(SceneState.Scene_ThankFadeIn, scene);
                    break;
                case 13:
                    
                    scene = new EndingNormalScene(1.2);
                    scene.SetLoadEndingScene(new NormalSpriteLoad("CloudG", "ending\\3\\18"));
                    m_EndingSceneDictionary.Add(SceneState.Scene7_2, scene);
                    break;
            }
        }
       
        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sb = m_GraphicData.GetSpriteBatch();
            m_EndingScene.Draw(sb);
           
        }
    }
}
